$(document).ready(function(ev) {
  
  $("select#status").change(function() {
    var $row = $(this).closest("tr.transaction_row");
    var id = $row.attr("transaction_id");
    var status = $(this).find(":selected").attr("value");
    $.ajax({
      url: "update_status",
      type: "PUT",
      beforeSend: function(xhr) { xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content')) },
      data: {
        id: id,
        status: status
      },
      success: function(responseJSON) {
        var transaction = responseJSON.transaction;
        $row.find("td.start_date").text(transaction.start_date);
        $row.find("td.end_date").text(transaction.end_date);
        alert("Updating status successfull.");
      },
      error: function(response) {
        var responseJSON = $.parseJSON(response.responseText);
        var errors = responseJSON.errors;
        alert(errors.join(". "));
      },
    });
  });

});

